<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * 路由规则抽像类
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
abstract class EGP_Controller_Router_Route_Abstract
{

    /**
     * 路径匹配路由
     *
     * @var string
     */
    protected $_route = null;

    /**
     * 路由规则生效的主机名
     *
     * @var array
     */
    protected $_hosts = array();

    /**
     * 存储路由默认值
     *
     * @var array
     */
    protected $_defaults = array();

    /**
     * 构造方法
     *
     * @param  array  $config
     */
    public function __construct($config = array())
    {
        if ($config instanceof EGP_ArrayObject) {
            $config = $config->toArray();
        }
        $this->addConfig($config);
    }

    /**
     * 添加路由配置
     *
     * @param  array  $config
     * @return EGP_Controller_Router_Route_Abstract
     */
    public function addConfig(array $config = array())
    {
        foreach ($config as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (!method_exists($this, $method)) {
                throw new EGP_Exception("不支持的路由参数 '$key'");
            }
            $this->$method($value);
        }

        return $this;
    }

    /**
     * 设置匹配路径的路由
     *
     * @param  string  $route
     * @return EGP_Controller_Router_Route_Abstract
     */
    public function setRoute($route)
    {
        $this->_route = ltrim($route, '/');

        return $this;
    }

    /**
     * 设置生效的主机名
     *
     * @param  string|array  $host
     * @return EGP_Controller_Router_Route_Abstract
     */
    public function setHosts($host)
    {
        if (!is_array($host)) {
            $host = array($host);
        }

        $host = array_map('strtolower', $host);
        $this->_hosts = array_merge($this->_hosts, $host);

        return $this;
    }

    /**
     * 是否在有效的主机名范围内
     *
     * @param  string  $host
     */
    protected function inHosts($host)
    {
        $host = strtolower($host);
        return empty($this->_hosts) || in_array($host, $this->_hosts)
                ? true : false;
    }

    /**
     * 批量设置默认值
     *
     * @param  array  $defaults
     * @return EGP_Controller_Router_Route_Abstract
     */
    public function setDefaults(array $defaults)
    {
        foreach ($defaults as $name => $value) {
            $this->setDefault($name, $value);
        }

        return $this;
    }

    /**
     * 设置默认值
     *
     * @param  string  $name
     * @param  mixed   $value
     * @return EGP_Controller_Router_Route_Abstract
     */
    public function setDefault($name, $value)
    {
        if (preg_match('/^[^\d]+$/', $name)) {
            $this->_defaults[$name] = $value;
        }

        return $this;
    }

    /**
     * 获取默认值
     *
     * @param  string  $name
     * @return mixed
     */
    public function getDefault($name)
    {
        return isset($this->_defaults[$name]) ? $this->_defaults[$name] : null;
    }

    /**
     * 获取所有默认值
     *
     * @return array
     */
    public function getDefaults()
    {
        return $this->_defaults;
    }

    /**
     * 测试是否匹配路径
     *
     * @param  string  $host
     * @param  string  $path
     * @return boolean
     */
    abstract public function match($host, $path);

}